import tool from "../../modules/tool"

// componnets/searchProduct/searchProduct.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    admin: false,
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    list: [],
    input: '',
    currentLeftIndex: -1,
    rightList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async handleChangeCurrentLeft(e) {
      e = e.currentTarget.dataset.index
      if (e == this.data.currentLeftIndex) return
      let list = tool.dcJSON(this.data.list[e])
      this.setData({
        rightList: []
      })
      await tool.wait(0.001)
      this.setData({
        currentLeftIndex: e,
        rightList: list.children
      })
    },
    async refresh() {
      let re = await tool.getProductList(this.data.input, this.properties.admin)
      if (re.status) {
        this.setData({
          list: re.data,
          show: true
        })
        this.setData({
          currentLeftIndex: -1
        })
        await tool.wait(0.001)
        await this.handleChangeCurrentLeft({
          currentTarget: {
            dataset: {
              index: 0
            }
          }
        })
      } else {
        tool.handleShowToast('获取失败')
      }
    },
    handleInput(e) {
      e = e.detail.value
      this.setData({
        input: e
      })
    },
    handleItemClick(e) {
      e = e.currentTarget.dataset.idvalue
      if (this.properties.admin) {
        tool.handleShowProductEditorPage(e)
      } else {
        tool.handleShowProductActivityDetailPage(true, e)
      }
    }
  },

  async created() {
    await tool.wait(0.01)
    let re = await tool.getProductList('', this.properties.admin)
    if (re.status) {
      this.setData({
        list: re.data,
        show: true
      })
      await tool.wait(0.001)
      await this.handleChangeCurrentLeft({
        currentTarget: {
          dataset: {
            index: 0
          }
        }
      })
    } else {
      tool.handleShowModel('无法获取商品列表，请尝试重启小程序')
    }
  }
})